package com.jet.chat.message;

import java.io.IOException;
import java.io.ObjectOutputStream;

import com.jet.chat.exception.ChatException;

public class MessageWriterObjectOutputStream implements MessageWriter {
	
	ObjectOutputStream writer;
	
	public MessageWriterObjectOutputStream(ObjectOutputStream writer) {
		this.writer = writer;
	}

	@Override
	public void write(Message message) throws ChatException {
		try {
			writer.writeObject(message);
		} catch (IOException e) {
			//throw new ChatException("throw new MessageWriterObjectOutputStream IOException writeObject");
			//throw new ChatException(e);
		}
		try {
			writer.flush();
		} catch (IOException e) {
			//throw new ChatException("throw new MessageWriterObjectOutputStream IOException flush");
			//throw new ChatException(e);
		}
	}

	@Override
	public void write(String string) throws ChatException {
		try {
			writer.writeObject(new Message(string));
		} catch (IOException e) {
			//throw new ChatException("throw new MessageWriterObjectOutputStream IOException writeObject");
			//throw new ChatException(e);
		}
		try {
			writer.flush();
		} catch (IOException e) {
			//throw new ChatException("throw new MessageWriterObjectOutputStream IOException flush");
			//throw new ChatException(e);
		}
	}

}
